$(function () {
    $('.select2').select2();
    var defaultColunms = BaseTable.initColumn();
    var table = new BSTable(BaseTable.id, "/wx/user/list", defaultColunms);
    table.setPaginationType("server");
    BaseTable.table = table.init();
});

/**
 * 表格类容初始化
 */
var BaseTable = {
    id: "table",	//表格id
    seItem: null,		//选中的条目
    table: null,
};

/**
 * 初始化表格的列
 */
BaseTable.initColumn = function () {
    return [
        {field: 'selectItem',checkbox : true},
        {field: 'id',title : '序号',align: 'center', valign: 'middle',formatter : function(value, row, index) {return index+1;}},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '昵称', field: 'nickname', align: 'center', valign: 'middle', },
        {title: '生日', field: 'birthday', align: 'center', valign: 'middle', },
        {title: '地区', field: 'region', align: 'center', valign: 'middle', },
        {title: '性别', field: 'gender', align: 'center', valign: 'middle',formatter: function(value, row, index){
            return value === 0 ? '女' : '男';
        }},
        {title: '身份', field: 'capacity', align: 'center', valign: 'middle',formatter: function(value, row, index){
            return getType(value);
        }},
        {title: '手机号', field: 'phone', align: 'center', valign: 'middle', },
        {title: '微信', field: 'weChat', align: 'center', valign: 'middle', },
        // {title: '微博', field: 'weibo', align: 'center', valign: 'middle', },
        {title: '状态', field: 'status', align: 'center', valign: 'middle', formatter: function(value, row, index){
            return value === 0 ?
                '<span class="label label-danger">禁用</span>' :
                '<span class="label label-success">正常</span>';
        }},
        // {title: '创建时间', field: 'createTime', align: 'center', valign: 'middle',},
        {title: '操作', field: 'status', align: 'center', valign: 'middle', formatter: function(value, row, index){
            var text = value == 0?'恢复':'禁用'
            var btnClazz = value == 0?'btn-success':'btn-danger'
            var html = '<div style="display: flex;justify-content: space-around">'
            html += '<a class="btn '+btnClazz+'" href="#" onclick="enabled('+row.id+','+value+')">'+text+'</a>';
            html += '<a class="btn btn-primary " href="#" onclick="getInfo('+row.id+')"><i class="glyphicon glyphicon-info-sign">详情</i></a>';
            html += '</div>'
            return html;
        }}
    ];
};

/**
 * 检查是否选中
 */
BaseTable.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        alert("请先选中表格中的某一记录！");
        return false;
    } else {
        BaseTable.seItem = selected[0];
        return true;
    }
};


/**
 * 查询表格列表
 */
BaseTable.search = function () {
    var queryData = {};
    queryData['key'] = $("#q").val();
    queryData['status'] = $("#status").val();
    BaseTable.table.refresh({query: queryData});
};

function enabled(id,value) {
    var ids = new Array();
    ids.push(id);
    var text = value == 0?'恢复':'禁用'
    showWarningMsg('确定要'+text+'该用户吗',function () {
        vm.saveOrUpdate(id,value)
    })
}

function getType(value) {
    return vm.getType(value)
}

function clearInput(obj) {
    $(obj).prev().val('');
}

function getInfo(id) {
    vm.getUser(id)
}

var vm = new Vue({
    el:'#app',
    data:{
        showList: true,
        data:{},
    },
    methods: {
        saveOrUpdate: function (id,status) {
            var params = {
                id:id,
                status:status == 0?1:0
            }
            ajaxSubmit("/wx/user/update",JSON.stringify(params),function (r) {
                if(r.code == 0){
                    showSuccessMsg('操作成功!');
                    vm.reload();
                }else{
                    alert(r.msg);
                }
            })
        },
        getUser: function(id){
            $.get(baseURL + "wx/user/getInfo/"+id, function(r){
                vm.data = r.user;
                vm.showList = false
            });
        },
        getType:function (value) {
            //1 摄影师 2模特 3化妆师 4造型师 5修图师 6商家 7素人模特 8其他
            switch (value) {
                case 1:
                    return '摄影师'
                case 2:
                    return '模特'
                case 3:
                    return '化妆师'
                case 4:
                    return '造型师'
                case 5:
                    return '修图师'
                case 6:
                    return '商家'
                case 7:
                    return '素人模特'
                default:
                    return '其他'
            }
        },
        reload: function () {
            vm.showList = true;

            BaseTable.search();
        },

    }
});
